<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/6/006
 * Time: 9:34
 */

namespace app\api\model;


use app\api\service\Token;
use app\lib\exception\CommentException;
use app\api\model\Order as OrderModel;
use app\api\model\OrderBuy as OrderBuyModel;
use app\api\model\PatternMasterComment as PatternMasterCommentModel;
use think\Db;
use think\Exception;

class ProductComment extends BaseModel {
    protected $hidden = ['update_time','delete_time', 'create_time', 'status', 'order_model', 'order_id', 'product_id', 'member_id'];
    /**
     * 评论用户
     * @return \think\model\relation\belongsTo
     */
    public function commentMember() {
        return $this->belongsTo('Member');
    }

    /**
     * 评论商品
     * @return \think\model\relation\belongsTo
     */
    public function product() {
        return $this->belongsTo('Product');
    }

    /**
     * 评论定制订单
     * @return \think\model\relation\belongsTo
     */
    public function commentOrder() {
        return $this->belongsTo('Order');
    }

    /**
     * 评论商城订单
     * @return \think\model\relation\belongsTo
     */
    public function commentOrderBuy() {
        return $this->belongsTo('OrderBuy');
    }

    /**
     * 根据产品ID获取评论列表
     * @param $id
     * @return array|\PDOStatement|string|\think\Collection
     */
    public static function getProductCommentByProductId($id) {
        $comments = self::with('commentMember.memberRoleInfo')
            ->where('product_id', '=', $id)
            ->where('status', '=', 1)
            ->paginate(15, true);

        foreach ($comments as $comment) {
            $comment->comment_member->visible(['member_role_info'])->member_role_info->visible(['nickname', 'headimgurl']);
        }

        return $comments;
    }

    /**
     * 添加评论信息
     * @param $param
     * @throws CommentException
     * @throws Exception
     */
    public static function addComment($param) {
        $uid = Token::getCurrentUid();

        $where = [
            'member_id' => $uid,
            'product_id' => $param['product_id'],
            'order_id' => $param['order_id'],
            'order_model' => $param['order_model'],
        ];

        $comment = self::get($where);
        if ($comment) {
            throw new CommentException([
                'msg' => '请勿重复提交评论',
                'errorCode' => 11001
            ]);
        }

        Db::startTrans();
        try {
            // 保存订单评论信息
            $data = [
                'member_id' => $uid,
                'product_id' => $param['product_id'],
                'order_id' => $param['order_id'],
                'order_model' => $param['order_model'],
                'level' => $param['level'],
                'content' => $param['content'],
                'img1' => isset($param['image'][0]) ? isset($param['image'][0]) : '',
                'img2' => isset($param['image'][1]) ? isset($param['image'][1]) : '',
                'img3' => isset($param['image'][2]) ? isset($param['image'][2]) : '',
            ];
            self::create($data);

            // 保存量体师评论信息
            if (isset($param['pm_comment_content']) && $param['order_model'] == 'order') {
                $order = OrderModel::get(['id' => $param['order_id'], 'member_id' => $uid]);
                $data = [
                    'member_id' => $uid,
                    'pattern_master_id' => $order['pattern_master_id'],
                    'order_id' => $param['order_id'],
                    'order_model' => 'order',
                    'level' => $param['pm_level'],
                    'content' => $param['pm_content'],
                    'img1' => isset($param['pm_image'][0]) ? isset($param['pm_image'][0]) : '',
                    'img2' => isset($param['pm_image'][1]) ? isset($param['pm_image'][1]) : '',
                    'img3' => isset($param['pm_image'][2]) ? isset($param['pm_image'][2]) : '',
                ];
                PatternMasterCommentModel::create($data);
            }

            // 修改订单评论状态
            if ($param['order_model'] == 'order') {
                OrderModel::update(['comment_status' => 1], ['id' => $param['order_id']]);
            } else {
                OrderBuyModel::update(['comment_status' => 1], ['id' => $param['order_id']]);
            }
            Db::commit();
        }catch (Exception $e) {
            Db::rollback();
            throw $e;
        }
    }
}